<?php
namespace app\api\model;

class Coze extends Base
{
    //扣子

    //生成日志
    public function logAdd($pas)
    {
        $pas['addtime'] = $this->toDate();
        return $this->dbConn($this->dbHhlog)->insertGetId($pas);
    }

    //更新日志
    public function logModify($id, $pas)
    {
        $pas['uptime'] = $this->toDate();
        return $this->dbConn($this->dbHhlog)->where('id', $id)->update($pas);
    }

    //获取会话
    public function hhOne($imei)
    {
        return $this->dbConn($this->dbHh)->where('imei', $imei)->find();
    }

    //创建会话
    public function hhAdd($hhid, $imei)
    {
        return $this->dbConn($this->dbHh)->insertGetId([
            'imei' => $imei, 'hh_id' => $hhid, 'addtime' => $this->toDate(),
        ]);
    }

    //创建对话
    public function userMsg($hhid, $chat_id, $cnt)
    {
        $res = $this->dbConn($this->dbHhchat)
            ->where('hh_id', $hhid)
            ->where('chat_id', $chat_id)
            ->find();
        if (!$res) {
            $lastid = $this->dbConn($this->dbHhchat)->insertGetId([
                'hh_id' => $hhid,'chat_id' => $chat_id, 'cnt' => $cnt,
                'addtime' => $this->toDate(),
            ]);
        } else {
            $lastid = $res['id'];
        }
        $msgOne = $this->dbConn($this->dbHhmsg)->where('chat_id', $chat_id)->find();
        if (!$msgOne) {
            $this->dbConn($this->dbHhmsg)->insertGetId([
                'chat_id' => $chat_id, 'role' => 'user', 'cnt' => $cnt,
                'addtime' => $this->toDate(),
            ]);
        }
        return $chat_id;
    }

    //获取对话
    public function hhGet($id)
    {
        $res = $this->dbConn($this->dbHhmsg)
            ->where('id', '>', $id)
            ->order('id', 'asc')
            ->find();
        if (!$res) {
            return $this->toback(-2, '没有记录');
        }
        $start = date('Y-m-d H:i:s', (time() - 5 * 60)); //5分钟前
        $is_door = 0;
        if (strpos($res['cnt'], '开门') && $res['addtime'] >= $start) {
//        if (strpos($res['cnt'], '开门') !== false) {
            $is_door = 1;
        }
        $is_bot = 0;
        if ($res['role'] != 'user') {
            $pOne = $this->dbConn($this->dbHhmsg)
                ->where('role', 'user')
                ->where('bot_id', 0)
                ->find();
            $is_bot = $pOne ? 0 : 1;
        }
        $arys = [
            'id' => $res['id'],
            'chat_id' => $res['chat_id'],
            'role' => $res['role']=='user' ? 1 : 2,
            'uname' => $res['role']=='user' ? 'U'.strtotime($res['addtime']) : '小嘉',
            'cnt' => $res['cnt'],
            'is_door' => $is_door,
            'is_bot' => $is_bot,
        ];
        return $this->toback(0, '', $arys);
    }

    //获取智能体问题
    public function msgOne()
    {
        $res = $this->dbConn($this->dbHhmsg)
            ->where('role', 'user')
            ->where('bot_id', 0)
            ->order('id', 'asc')
            ->find();
        if (!$res) {
            return $this->toback(-2, '没有记录');
        }
        $chatOne = $this->dbConn($this->dbHhchat)
            ->where('chat_id', $res['chat_id'])
            ->find();
        $res['hh_id'] = $chatOne ? $chatOne['hh_id'] : 0;
        return $this->toback(0, '', $res);
    }

    //智能体回复
    public function botMsg($chat_id, $msg)
    {
        $bot_id = $this->dbConn($this->dbHhbot)->insertGetId([
            'chat_id' => $chat_id, 'cnt' => $msg,
            'addtime' => $this->toDate(),
        ]);
        $this->dbConn($this->dbHhmsg)
            ->where('chat_id', $chat_id)
            ->where('role', 'user')
            ->update(['bot_id' => $bot_id]);
        $this->dbConn($this->dbHhmsg)->insertGetId([
            'chat_id' => $chat_id, 'role' => 'assistant', 'cnt' => $msg,
            'addtime' => $this->toDate(),
        ]);
        return true;
    }
}